﻿Imports System.Text

Public Class Code
    Private _ParamsValues As New Hashtable
    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        Dim ttf As wys.Control.Vb.TreeTableFromBase = New wys.Control.Vb.TreeTableFromBase(Me.txtParentName.Tag.ToString())
        ttf.ID = "ID"
        ttf.ParentID = "ParentID"
        ttf.DisplayName = "Name"
        ttf.TableName = "XscBugType"
        ttf.Text = "语言类型管理"
        ttf.TypeId = 2
        ttf.ExpandChild = False
        ttf.ShowDialog()
        If (Not ttf.Cancel) Then
            txtParentName.Text = ttf.DisplayNameValue
            txtParentName.Tag = ttf.Result
        End If
        txtParentName.Text = ct.GetParentName(txtParentName.Tag.ToString(), "XscBugType", "Name", "ParentID", "ID")
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        _ParamsValues.Clear()
        _ParamsValues.Add("txtParentName", txtParentName.Tag)
        Dim dt As DataTable = CreateDataSourceCode()
        If dt.Rows.Count = 0 Then
            ShowMessageBox("没有数据")
            Return
        End If

        Try

            Dim englishFolder As String = System.IO.Path.Combine(GetCurrentDirectory, "Report")
            If Not System.IO.Directory.Exists(englishFolder) Then
                System.IO.Directory.CreateDirectory(englishFolder)
            End If

            Dim englishPath As String = System.IO.Path.Combine(englishFolder, "SoftWareDetil.xls")
            Dim bCreate As Boolean = True
            If System.IO.File.Exists(englishPath) Then
                System.IO.File.Delete(englishPath)
            End If

            For jndex As Integer = 0 To dt.Rows.Count - 2
                For kdex As Integer = jndex + 1 To dt.Rows.Count - 1
                    If dt.Rows(jndex)("语言") = dt.Rows(kdex)("语言") Then
                        dt.Rows(kdex)("语言") = ""
                    End If
                Next
            Next

            If rad详细.Checked Then
                Dim englishCreate As New CodeD(englishPath)
                englishCreate.报表记录条数字 = Me.报表记录条数字
                englishCreate.报表字体 = Me.报表字体

                AddHandler englishCreate.PrintProcess, AddressOf PrintProcess

                englishCreate.DoOutData(dt)
                RemoveHandler englishCreate.PrintProcess, AddressOf PrintProcess
            Else
                Dim englishCreate As New CodeE(englishPath)
                englishCreate.报表记录条数字 = Me.报表记录条数字
                englishCreate.报表字体 = Me.报表字体

                AddHandler englishCreate.PrintProcess, AddressOf PrintProcess

                englishCreate.DoOutData(dt)
                RemoveHandler englishCreate.PrintProcess, AddressOf PrintProcess
            End If

            '打开文件
            Using pro As New Process
                pro.StartInfo.FileName = englishPath
                pro.Start()
            End Using

        Catch ex As Exception
            ShowMessageBox(ex.ToString)
        End Try

    End Sub

    Private Sub PrintProcess(ByVal content As String)
        lbl1000.Text = content
        System.Windows.Forms.Application.DoEvents()
    End Sub

#Region "创建代码数据源"

    Private Function CreateDataSourceCode() As DataTable

        '声明表当数据源
        Dim dt As New DataTable("XSCCODE")
        '连接查询数据
        Dim sb As New StringBuilder

        Dim typeIds As New List(Of String)
        Dim strSql As String

        Dim newTable As New DataTable
        If rad统计.Checked Then
            newTable.Columns.Add("条数")
            newTable.Columns.Add("语言")
        Else
            newTable.Columns.Add("标题")
            newTable.Columns.Add("语言")
        End If

        If String.IsNullOrEmpty(_ParamsValues.Item("txtParentName").ToString()) Then
            strSql = "select * from XscBugType Where type=2 and ParentID is null"
        Else
            strSql = "select * from XscBugType Where type=2 and ParentID='" + _ParamsValues.Item("txtParentName").ToString() + "'"
        End If

        Dim rs As ADODB.Recordset = ct.GetTable(strSql)
        Dim lstct As New List(Of String)
        Dim strSql2 As String = ""
        Dim rs1 As ADODB.Recordset = Nothing

        If rs IsNot Nothing Then
            While Not rs.EOF
                lstct.Clear()
                lstct = ct.GetChildIdAndSelf(rs.Fields("id").Value.ToString(), "2")
                strSql2 = String.Join(",", lstct.ToArray())
                If rad统计.Checked Then
                    strSql2 = "select count(*) as id ,'" + rs.Fields("Name").Value.ToString() + "' as LanagerName from xsccode where type in(" + strSql2 + ")"
                Else
                    strSql2 = "select title as id ,'" + rs.Fields("Name").Value.ToString() + "' as LanagerName from xsccode where type in(" + strSql2 + ")"
                End If
                rs1 = ct.GetTable(strSql2)
                If rad统计.Checked Then
                    newTable.Rows.Add(New String() {Int32.Parse(rs1.Fields("id").Value), rs1.Fields("LanagerName").Value.ToString()})
                Else
                    While rs1.EOF = False
                        newTable.Rows.Add(New String() {rs1.Fields("id").Value, rs1.Fields("LanagerName").Value.ToString()})
                        rs1.MoveNext()
                    End While
                End If
                
                rs1.Close()
                rs1 = Nothing
                rs.MoveNext()
            End While
            rs.Close()
            rs = Nothing
        End If
        'A-2010-1-2      

        Return newTable

    End Function
#End Region

#Region "报表必须使用"
    Private _报表字体 As String = "宋体"
    Public Overrides Property 报表字体() As String
        Get
            Return _报表字体
        End Get
        Set(ByVal value As String)
            _报表字体 = value
        End Set
    End Property

    Private _报表记录条数字 As Integer = 56
    Public Overrides Property 报表记录条数字() As Integer
        Get
            Return _报表记录条数字
        End Get
        Set(ByVal value As Integer)
            _报表记录条数字 = value
        End Set
    End Property
#End Region

    Private Sub Code_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtParentName.Tag = ""
    End Sub
End Class
